//
//  ThreeLevelLinkageModel.m
//  huashiedu
//
//  Created by oceans on 2018/9/9.
//  Copyright © 2018年 hema. All rights reserved.
//

#import "ThreeLevelLinkageModel.h"
#import "JFAreaDataManager+ThreeLevelLinkage.h"

@implementation ProvinceModel

- (instancetype)initWithDic:(NSDictionary *)dic {
    self = [super init];
    if (self) {
        _province_name = [dic objectForKey:@"province_name"];
        _province_number = [dic objectForKey:@"province_number"];
    }
    return self;
}
@end

@implementation CityModel

- (instancetype)initWithDic:(NSDictionary *)dic {
    self = [super init];
    if (self) {
        _city_number = [dic objectForKey:@"city_number"];
        _city_name = [dic objectForKey:@"city_name"];
    }
    return self;
}
@end

@implementation AreaModel

- (instancetype)initWithDic:(NSDictionary *)dic {
    self = [super init];
    if (self) {
        _area_name = [dic objectForKey:@"area_name"];
        _area_number = [dic objectForKey:@"area_number"];
    }
    return self;
}
@end

static ThreeLevelLinkageModel *shared;
@implementation ThreeLevelLinkageModel

+ (instancetype)shared {
    static dispatch_once_t token;
    dispatch_once(&token, ^{
        shared = [[self alloc] init];
    });
    return shared;
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    static dispatch_once_t token;
    dispatch_once(&token, ^{
        shared = [super allocWithZone:zone];
    });
    return shared;
}

- (id)copyWithZone:(NSZone *)zone {
    return shared;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        [self loadProvince];
    }
    return self;
}

- (void)loadProvince {
    __weak typeof(self) weakself = self;
    [[JFAreaDataManager shareInstance] areaSqliteDBData];
    [[JFAreaDataManager shareInstance] provinceData:^(NSMutableArray *dataArray) {
        NSMutableArray *provinces = [NSMutableArray arrayWithCapacity:dataArray.count];
        for (NSDictionary *dic in dataArray) {
            ProvinceModel *model = [[ProvinceModel alloc] initWithDic:dic];
            [provinces addObject:model];
        }
        weakself.provinces = provinces;
    }];
}

- (void)didSelectProvinceAtIndex:(NSInteger)index cityData:(void (^)(NSMutableArray<CityModel *> *))cityDataBlock areaData:(void (^)(NSMutableArray<AreaModel *> *))areaDataBlock {
    
    __weak typeof(self) weakself = self;
    [[JFAreaDataManager shareInstance] cityData:_provinces[index].province_number cityData:^(NSMutableArray *cityData) {
       
        NSMutableArray <CityModel *> *citys = [NSMutableArray arrayWithCapacity:cityData.count];
        for (NSDictionary *dic in cityData) {
            CityModel *model = [[CityModel alloc] initWithDic:dic];
            [citys addObject:model];
        }
        if (cityDataBlock) {
            cityDataBlock(citys);
        }
        if (areaDataBlock) {
            [weakself didSelectCityAtIndex:citys[0].city_number areaData:areaDataBlock];
        }
    }];
}

- (void)didSelectCityAtIndex:(NSString *)city_number areaData:(void (^)(NSMutableArray<AreaModel *> *))areaDataBlock {
    
    [[JFAreaDataManager shareInstance] selectAreaData:city_number areaData:^(NSMutableArray *areaData) {
        NSMutableArray *areas = [NSMutableArray arrayWithCapacity:areaData.count];
        for (NSDictionary *dic in areaData) {
            AreaModel *model = [[AreaModel alloc] initWithDic:dic];
            [areas addObject:model];
        }
        if (areaDataBlock) {
            areaDataBlock(areas);
        }
    }];
}

@end
